import http from '../core/request'
import { IAuth } from './user.type'
import { Session } from '#/entity'

// 获取当前登录用户的所有权限
const getRules = () => {
  return http.get<IAuth.RulesResult[]>('/session/has_rules', {}, { loading: false })
}

// 获取验证图片
const getVerifyImage = () => {
  return http.get<IAuth.verifyRes>('/captcha', {}, { loading: false })
}

// 判断用户是否已存在
const getOpenId = (params: IAuth.Params.GetOpenId) => {
  return http.post<IAuth.Result>('/wechat/js-code/sign-in', params, { loading: false })
}

// 一键登录
const login = (params: IAuth.Params.Login) => {
  return http.post<IAuth.Result>('/wechat/login-must-exist', params, { loading: true })
}

// 账号密码登录(query参数)
const codeLogin = (params: IAuth.Params.Login) => {
  return http.post<IAuth.Result>(`/user/code/sign_in?app=${params.app}&uuid=${params.uuid}&captcha=${params.captcha}&code=${params.code}&password=${params.password}`, {}, { loading: true })
}

// 退出登录
const logout = () => {
  return http.post('/user/sign_out', {}, { loading: true })
}

// 获取当前登录的用户信息
const getCurrentUserInfo = () => {
  return http.get<Session>(`/session`, {}, { loading: false })
}

// 获取用户信息（任何人）
const getUserInfo = (id: number) => {
  return http.get<Session>(`/customer-user/${id}/user-detail`, {}, { loading: false })
}

// 修改用户头像
const updateUserAvatar = (params: IAuth.Params.UpdateAvatar) => {
  return http.post<IAuth.Result>('/user/portrait', params, { loading: true })
}

// 修改用户名称
const updateUserName = (params: IAuth.Params.UpdateName) => {
  return http.post<IAuth.Result>('/user/name', params, { loading: true })
}

// 获取用户推广二维码
const getPromoteCode = () => {
  return http.get<IAuth.PromoteCode>('/qxt/get-bound-picture', {}, { loading: false })
}

// 获取背景图
const getPromotionImage = () => {
  return http.get<{ file: string }>('/promotion', {}, { loading: false })
}
// 获取短链接
const getShortUrl = () => {
  return http.get<IAuth.ShortUrl>('/express/short', {}, { loading: false })
}
// 根据工号获取客服信息
const getCustomerInfo = (params: { purchaserId: string }) => {
  return http.get<IAuth.CustomerInfo>('/qxt/sys-user/purchaser', params, { loading: false })
}
export default {
  getRules,
  getVerifyImage,
  getOpenId,
  login,
  codeLogin,
  logout,
  getCurrentUserInfo,
  getUserInfo,
  updateUserAvatar,
  updateUserName,
  getPromoteCode,
  getPromotionImage,
  getShortUrl,
  getCustomerInfo
}
